package org.zht.dormpro.dorm.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.model.TbCollege;
import org.zht.dormpro.dorm.service.ICollegeService;
import org.zht.dormpro.pub.ResponseMap;

import java.util.*;

@Controller
@RequestMapping("/dorm/college")
public class CollegeController {

    @Autowired
    private ICollegeService collegeService;

    /**
     * 得到所有学院列表
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/listCollege")
    @RequiresPermissions("dorm:list:all")
    public ResponseMap listCollege(@RequestParam Map<String,Object> params){
        PageBean pageBean = new PageBean(params);
        List<TbCollege> colleges = collegeService.getCollegeByCondition(pageBean);
        Map<String, Object> attr = new HashMap<>();
        attr.put("total", pageBean.getTotal());
        return ResponseMap.success(colleges, attr);
    }


    /**
     * 跳转到添加学院页面
     * @return
     */
    @RequestMapping("/addCollegePage")
    public String addCollegePage() {

        return "/college/college_add";
    }

    /**
     * 跳转到学院列表页面
     * @return
     */
    @RequestMapping("/collegePage")
    public String collegePage(){
        return "/college/college_list";
    }

    /**
     * 添加学院信息
     * @param tbCollege
     * @return
     */
    @RequestMapping(value="/addCollege" ,method= RequestMethod.POST)
    @ResponseBody
    public ResponseMap addCollege(TbCollege tbCollege){
        boolean success = collegeService.addCollege(tbCollege);
        if(success){
            return ResponseMap.success("学院添加成功");
        }else{
            return ResponseMap.error("学院添加失败");
        }
    }

    /**
     * 跳转到编辑学院页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/collegeEditPage/{id}")
    public String editCollegePage(Model model,@PathVariable("id") Integer id){
        TbCollege tbCollege = collegeService.getCollegeById(id);
        model.addAttribute("college",tbCollege);
        return "/college/college_edit";
    }


    /**
     * 编辑学院信息
     * @param tbCollege
     * @return
     */
    @RequestMapping(value="/editCollege",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap editCollege(TbCollege tbCollege){
        boolean success = collegeService.editCollege(tbCollege);
        if(success){
            return ResponseMap.success("学院修改成功");
        }else {
            return ResponseMap.error("学院修改失败");
        }
    }

    //删除学院信息
    @RequestMapping(value = "/deleteCollege",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap deleteCollege(Integer id){
        boolean success = collegeService.deleteCollege(id);
        if(success){
            return ResponseMap.success("学院删除成功");
        }else{
            return ResponseMap.error("学院删除失败");
        }
    }

    /**
     * 批量删除学院信息
     * @param colIds
     * @return
     */
    @RequestMapping(value = "/batchRemoveCollege",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap batchRemoveCollege(@RequestParam("colIds[]") String[] colIds){
        boolean success = collegeService.batchRemoveCollege(new ArrayList<String>(Arrays.asList(colIds)));
        if(success){
            return ResponseMap.success("学院批量删除成功");
        }else{
            return ResponseMap.error("学院批量删除失败");
        }
    }
}
